package com.feiyi.app.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;


import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.feiyi.app.MainActivity;
import com.feiyi.app.R;
import com.feiyi.app.adapter.goodsdetail.ItemTitlePagerAdapter;
import com.feiyi.app.app.AppConstant;
import com.feiyi.app.base.BaseActivity;

import com.feiyi.app.bean.GoodsAddToCartBean;
import com.feiyi.app.fragment.goodsdetail.GoodsCommentFragment;
import com.feiyi.app.fragment.goodsdetail.GoodsDetailFragment;
import com.feiyi.app.fragment.goodsdetail.GoodsInfoFragment;
import com.feiyi.app.net.HttpMethods;
import com.feiyi.app.view.goodsdetail.NoScrollViewPager;
import com.gxz.PagerSlidingTabStrip;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.Observer;
import io.reactivex.Scheduler;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import okhttp3.FormBody;
import okhttp3.RequestBody;

/**
 * 作者 : wlt
 * 创建时间:  2018-07-18
 * QQ : 1346947404
 * 邮箱:wanglt_android@163.com
 * 类说明:商品详情页
 * In me the tiger sniffs the rose
 */

public class GoodsDetailActivity extends BaseActivity {

    @BindView(R.id.title_tabs)
    PagerSlidingTabStrip titleTabs;
    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.viewPager)
    NoScrollViewPager viewPager;
    @BindView(R.id.tv_goods_detail_focus)
    TextView tv_focus;


    private List<Fragment> fragmentList = new ArrayList<>();
    private GoodsInfoFragment goodsInfoFragment=new GoodsInfoFragment();
    private GoodsDetailFragment goodsDetailFragment=new GoodsDetailFragment();
    private GoodsCommentFragment goodsCommentFragment=new GoodsCommentFragment();
    private String[] titles= new String[]{"商品", "详情", "评价"};

    private String uid;
    private String sid;
    private String goods_id;
    private static final String TAG = "main";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_goods_detail);
        ButterKnife.bind(this);
        init();
    }
    private void init() {
        goods_id = getIntent().getStringExtra("goods_id");

        uid = spUtils.getString(AppConstant.UID);
        sid = spUtils.getString(AppConstant.SID);

        Bundle bundle = new Bundle();
        bundle.putString("goods_id",goods_id);
        Log.e("main Activity", "init: "+goods_id );
        goodsInfoFragment.setArguments(bundle);
        fragmentList.add(goodsInfoFragment);
        goodsDetailFragment.setArguments(bundle);
        fragmentList.add(goodsDetailFragment );
        goodsCommentFragment.setArguments(bundle);
        fragmentList.add(goodsCommentFragment);
        viewPager.setAdapter(new ItemTitlePagerAdapter(getSupportFragmentManager(),
                fragmentList, titles));
        viewPager.setOffscreenPageLimit(3);
        titleTabs.setViewPager(viewPager);
    }
    public void operaTitleBar(boolean scroAble,boolean titleVisiable,boolean tanVisiable){
        viewPager.setNoScroll(scroAble);
        title.setVisibility(titleVisiable? View.VISIBLE:View.GONE);
        titleTabs.setVisibility(tanVisiable? View.VISIBLE:View.GONE);
    }
    @OnClick({R.id.back,R.id.rl_focus,R.id.rl_shopping_cart,R.id.rl_buy_now})
    public void onClick(View view){
        if (view.getId() == R.id.back){
            finish();
        }
        if (view.getId() == R.id.rl_focus){
            if (tv_focus.getText().equals("关注")){
                ToastUtils.showShort("已关注");
                tv_focus.setText("已关注");
            }else {
                ToastUtils.showShort("关注");
                tv_focus.setText("关注");

            }
        }
        if (view.getId() == R.id.rl_shopping_cart){
            finish();
            Intent intent = new Intent(this,MainActivity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
            intent.putExtra("shop_cart","y");
            startActivity(intent);
        }
        if(view.getId() == R.id.rl_buy_now){
           if (null == sid || null == uid){
               ToastUtils.showShort("请登录");
               ActivityUtils.startActivity(LoginActivity.class);
           }else {
               addToCart();
           }
        }

    }
    private void addToCart(){
        uid = spUtils.getString(AppConstant.UID);
        sid = spUtils.getString(AppConstant.SID);
        if (null != sid && null != uid){
            Log.e(TAG, "addToCart: "+uid+"__"+sid );
        }
        FormBody.Builder builder = new FormBody.Builder();
        builder.add("sid",sid);
        builder.add("uid",uid);
        builder.add("goods_id",goods_id);
        builder.add("number","1");
        RequestBody body = builder.build();
        HttpMethods.getInstance().info.addToCart(AppConstant.ADD_TO_CART_URL,body)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<GoodsAddToCartBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        addDisposable(d);
                    }

                    @Override
                    public void onNext(GoodsAddToCartBean goodsAddToCartBean) {
                        if (goodsAddToCartBean.getStatus().getSucceed() == 1){
                            ToastUtils.showShort("加入购物车成功");
                            EventBus.getDefault().post("change");
                        }else {
                            ToastUtils.showShort("加入购物车失败,"+goodsAddToCartBean.getStatus().getError_desc());
                            ActivityUtils.startActivity(LoginActivity.class);
                        }
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });

    }
}
